const mysql = require('mysql2')
require('dotenv').config()
let poolObj = {}
// 开发环境
if (process.env.ENV === 'dev') {
  poolObj = {
    host: process.env.DB_HOST_DEV,
    user: process.env.DB_USER_DEV,
    password: process.env.DB_PASSWORD_DEV,
    database: process.env.DB_NAME_DEV,
    waitForConnections: true,
    connectionLimit: 10,
    queueLimit: 0,
  }
}
// 生产环境
else {
  poolObj = {
    host: process.env.DB_HOST,
    user: process.env.DB_USER,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME,
    waitForConnections: true,
    connectionLimit: 10,
    queueLimit: 0,
  }
}

const pool = mysql.createPool({
  ...poolObj,
})

module.exports = pool.promise()
